package hoyo.zqy.basic.service.impl;

import hoyo.zqy.basic.config.Log;
import hoyo.zqy.basic.domain.BaseDomain;
import hoyo.zqy.basic.mapper.BaseMapper;
import hoyo.zqy.basic.query.BaseQuery;
import hoyo.zqy.basic.service.IBaseService;
import hoyo.zqy.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//查询的方法比较多，使用查询的事务配置
//只读，事务传播机制=SUPPORTS，有我就用，没有事务就不用
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    /**
     * 保存
     * @param t
     */
    @Override
    public void save(T t) {
        baseMapper.save(t);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    /**
     * 修改
     * @param t
     */
    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    /**
     * 查询id
     * @param id
     * @return
     */
    @Override
    public T loadById(Long id) {
        return baseMapper.loadById(id);
    }

    /**
     * 查询全部
     * @return
     */
    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }

    /**
     * 分页查询
     * @param query
     * @return
     */
    @Override
    public PageList<T> queryPage(BaseQuery query) {
        //查询总条数
        Integer totals = baseMapper.queryCount(query);
        //判断是否有数据
        if (totals==null){
            //没数据直接返回
            return new PageList<>();
        }
        //查询分页数据
        List<T> data = baseMapper.queryData(query);
        return new PageList<>(totals,data);

    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void deleBatch(Long[] ids) {
        baseMapper.deleBatch(ids);
    }
}
